﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

using Daybreak.Core;
using Daybreak.Core.Rendering;
using Daybreak.Core.SceneGraph;
using Daybreak.Core.Input;

namespace Tutorials
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class TutorialApp : Game
    {
        protected GraphicsDeviceManager graphics;
        protected ContentManager content;
        protected Engine xnaEngine;
        protected Scene scene;
        protected FpsCounter fpsCounter;

        SpriteBatch batch;
        SpriteFont font;


        public TutorialApp()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.SynchronizeWithVerticalRetrace = false;
            graphics.PreferMultiSampling = false;
            graphics.ApplyChanges();

            IsFixedTimeStep = false;
            content = new ContentManager(Services);

            fpsCounter = new FpsCounter(this);
            Components.Add(fpsCounter);


        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {

            base.Initialize();


            Window.Title = "Daybreak motor";

        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            batch = new SpriteBatch(graphics.GraphicsDevice);
            font = content.Load<SpriteFont>("Fonts/default");

            xnaEngine = new Engine(graphics);

            xnaEngine.Keyboard.KeysDown += new Daybreak.Core.Input.KeyEventHandler(Keyboard_KeysDown);

            scene = new Scene("test", "unnamed_scene");

            xnaEngine.Camera = new MouseCamera(1);
            xnaEngine.Camera.Position = new Vector3(20, 10, 20);
            xnaEngine.Camera.RotationDegrees = new Vector2(10, 230);


        }

        void Keyboard_KeysDown(KeyboardDevice sender, KeyEventArgs e)
        {
            if(e.ContainsKey(Keys.Escape))
                this.Exit();
            
            if(e.ContainsKey( Keys.F1) )
                Engine.DoZSorting = !Engine.DoZSorting;


        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            xnaEngine.Tick();

            base.Draw(gameTime);

            DrawFrame(gameTime);
        }

        protected virtual void DrawFrame(GameTime gameTime)
        {

        }

        protected void WriteText(string txt, float x, float y, Color col)
        {
            Vector2 pos = new Vector2(x, y);

            batch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
            batch.DrawString(font, txt, pos, col);
            batch.End();
        }
    }
}
